package com.gxa.jjty.cjr.controller;


import com.gxa.jjty.cjr.mapper.OrderoRecordMapper;
import com.gxa.jjty.cjr.service.BillService;
import com.gxa.jjty.cjr.service.CjrUserService;
import com.gxa.jjty.cjr.service.OrderoRecordService;
import com.gxa.jjty.cjr.service.TorderService;
import com.gxa.jjty.core.common.ExceptionCode;
import com.gxa.jjty.core.common.JsonResult;
import com.gxa.jjty.core.exception.BusinessException;
import com.gxa.jjty.core.pojo.CjrUser;
import com.gxa.jjty.core.pojo.OrderoRecord;
import com.gxa.jjty.core.pojo.ServerAndTorderVo;
import com.gxa.jjty.core.pojo.Torder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

/**
 * @author wanjun
 * @version 1.0
 * @date 2021/12/1 20:22
 */
@Log4j2
@Api(tags = "前台订单接口")
@RestController
@RequestMapping("/Order")
@Validated
@Transactional(rollbackFor = Exception.class)
public class TorderController {

    @Autowired
    private TorderService torderService;
    @Autowired
    private OrderoRecordService orderoRecordService;
    @Autowired
    private BillService billService;
    @Autowired
    private CjrUserService cjrUserService;

    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderAddress",value = "服务地址(必须)",required = true),
            @ApiImplicitParam(name = "orderUser",value = "姓名(必须)",required = true),
            @ApiImplicitParam(name = "orderPhone",value = "电话(必须)",required = true),
            @ApiImplicitParam(name = "cjrId",value = "(必须)",required = true),
            @ApiImplicitParam(name = "serveType",value = "服务类型(必须)",required = true),
            @ApiImplicitParam(name = "orderPrice",value = "订单价格(必须)",required = true),
            @ApiImplicitParam(name = "orderRemark",value = "订单备注(可选)"),
            @ApiImplicitParam(name = "bookTime",value = "预约时间(可选)")
    })
    @ApiOperation("添加订单的方法(将订单挂起，待付款)")
    @PostMapping("/insert1")
    public Map<String,Object> addTorder1( Torder torder){

        torderService.addTorder1(torder);

        return new JsonResult("1","成功","").getDataMap();
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderAddress",value = "服务地址(必须)",required = true),
            @ApiImplicitParam(name = "orderUser",value = "姓名(必须)",required = true),
            @ApiImplicitParam(name = "orderPhone",value = "电话(必须)",required = true),
            @ApiImplicitParam(name = "cjrId",value = "(必须)",required = true),
            @ApiImplicitParam(name = "serveType",value = "服务类型(必须)",required = true),
            @ApiImplicitParam(name = "orderPrice",value = "订单价格(必须)",required = true),
            @ApiImplicitParam(name = "orderRemark",value = "订单备注(可选)"),
            @ApiImplicitParam(name = "bookTime",value = "预约时间(可选)")
    })
    @ApiOperation("添加订单的方法(免费，直接完成支付)")
    @PostMapping("/insert2")
    public Map<String,Object> addTorder2(Torder torder) throws BusinessException {
        CjrUser cjrUser = cjrUserService.selectUser(torder.getOrderPhone());
        if (cjrUser.getFrees()<0) {
            throw new BusinessException(ExceptionCode.FREE_NULL);
        }
        torder.setPayPrice(0.0);
        torder.setPayType("免费");
        torderService.addTorder2(torder);
        billService.income(torder.getPayPrice(),torder.getOrderNumber());
        Integer user=cjrUser.getFrees()-1;
        cjrUser.setFrees(user);
        cjrUserService.updateUser(cjrUser);
        OrderoRecord orderoRecord = new OrderoRecord();
        orderoRecord.setOrderId(torder.getOrderId());
        orderoRecord.setOperateContent("添加订单(免费，直接完成支付)");
        orderoRecord.setOperateRemark(torder.getCancelRemark());
        orderoRecord.setOperateUser(torder.getOrderUser());
        orderoRecordService.addRecord(orderoRecord);
        return new JsonResult("1","成功","").getDataMap();
    }@ApiImplicitParams({
            @ApiImplicitParam(name = "orderAddress",value = "服务地址(必须)",required = true),
            @ApiImplicitParam(name = "orderUser",value = "姓名(必须)",required = true),
            @ApiImplicitParam(name = "orderPhone",value = "电话(必须)",required = true),
            @ApiImplicitParam(name = "cjrId",value = "(必须)",required = true),
            @ApiImplicitParam(name = "serveType",value = "服务类型(必须)",required = true),
            @ApiImplicitParam(name = "orderPrice",value = "订单价格(必须)",required = true),
            @ApiImplicitParam(name = "orderRemark",value = "订单备注(可选)"),
            @ApiImplicitParam(name = "bookTime",value = "预约时间(可选)")
    })
    @ApiOperation("添加订单的方法(微信，支付宝，直接完成支付)")
    @PostMapping("/insert3")
    public Map<String,Object> addTorder3(Torder torder) throws BusinessException {
        torder.setPayPrice(torder.getOrderPrice());
        torderService.addTorder2(torder);
        billService.income(torder.getPayPrice(),torder.getOrderNumber());
        OrderoRecord orderoRecord = new OrderoRecord();
        orderoRecord.setOrderId(torder.getOrderId());
        orderoRecord.setOperateContent("添加订单的方法(微信，支付宝，直接完成支付)");
        orderoRecord.setOperateRemark(torder.getCancelRemark());
        orderoRecord.setOperateUser(torder.getOrderUser());
        orderoRecordService.addRecord(orderoRecord);
        return new JsonResult("1","成功","").getDataMap();
    }

    @ApiOperation("查询待支付订单")
    @PostMapping("/select")
    public Map<String,Object> getTorderPaid(@RequestParam("orderNumber") String number){
        List<ServerAndTorderVo> torderBePaid = torderService.getTorderBePaid(number);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(torderBePaid);
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }

    @ApiOperation("取消订单")
    @PostMapping("/delete")
    public Map deleteTOrder(@RequestParam("number") String number,@RequestParam(value = "cancelRemark",required = false) String cancelRemark) throws BusinessException {
        List<Torder> torders = torderService.selectByNumbers(number);
        if (torders.size() < 0) {
            throw  new BusinessException(ExceptionCode.TORDER_NULL);
        }
        Torder torder = torders.get(0);
        OrderoRecord orderoRecord = new OrderoRecord();
        orderoRecord.setOrderId(torder.getOrderId());
        orderoRecord.setOperateContent("取消订单");
        orderoRecord.setOperateRemark(torder.getCancelRemark());
        orderoRecord.setOperateUser(torder.getOrderUser());
        orderoRecordService.addRecord(orderoRecord);
        torderService.deleteTorder(number,cancelRemark);
        return new JsonResult("1","成功","").getDataMap();
    }

    @ApiOperation("支付订单(免费)")
    @PostMapping("/update1")
    public Map updateTOrder1(@RequestParam("number") String number,@RequestParam("orderPhone") String phone) throws BusinessException {
        List<Torder> torders = torderService.selectByNumbers(number);
        if (torders.size() <= 0) {
            throw new BusinessException(ExceptionCode.TORDER_NULL);
        }
        CjrUser cjrUser = cjrUserService.selectUser(phone);
        if (cjrUser.getFrees()<0) {
            throw new BusinessException(ExceptionCode.FREE_NULL);
        }
        Torder torder1 = torders.get(0);
        JsonResult jsonResult = new JsonResult();
        List<ServerAndTorderVo> torderBePaid = torderService.getTorderBePaid(phone);
        for (ServerAndTorderVo torder : torderBePaid) {
            if (number.equals(torder.getOrderNumber())){
                billService.income(0.0,number);
            }
        }
        torderService.updateTorderFree(number);
        Integer user=cjrUser.getFrees()-1;
        cjrUser.setFrees(user);
        cjrUserService.updateUser(cjrUser);
        OrderoRecord orderoRecord = new OrderoRecord();
        orderoRecord.setOrderId(torder1.getOrderId());
        orderoRecord.setOperateContent("支付订单(免费)");
        orderoRecord.setOperateRemark(torder1.getCancelRemark());
        orderoRecord.setOperateUser(torder1.getOrderUser());
        orderoRecordService.addRecord(orderoRecord);
        jsonResult.setCode("1");
        return  jsonResult.getDataMap();
    }
    @ApiOperation("支付订单(微信，支付宝支付)")
    @PostMapping("/update")
    public Map updateTOrder(Torder torder) throws BusinessException {

        JsonResult jsonResult = new JsonResult();

        List<ServerAndTorderVo> torderBePaid = torderService.getTorderBePaid(torder.getOrderPhone());
        for (ServerAndTorderVo torder1 : torderBePaid) {
            if (torder.getOrderNumber().equals(torder1.getOrderNumber())){
                billService.income(torder1.getOrderPrice(),torder.getOrderNumber());
            }
        }
        torderService.updateTorder(torder.getOrderNumber());
        jsonResult.setCode("1");
        OrderoRecord orderoRecord = new OrderoRecord();
        orderoRecord.setOrderId(torder.getOrderId());
        orderoRecord.setOperateContent("支付订单(微信，支付宝支付)");
        orderoRecord.setOperateRemark(torder.getCancelRemark());
        orderoRecord.setOperateUser(torder.getOrderUser());
        orderoRecordService.addRecord(orderoRecord);
        return  jsonResult.getDataMap();
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId",value = "订单id(必须)",required = true),
            @ApiImplicitParam(name = "operateUser",value = "操作人(必须)",required = true),
            @ApiImplicitParam(name = "operateContent",value = "操作内容(必须)",required = true),
            @ApiImplicitParam(name = "operateRemark",value = "操作备注"),
    })
    @ApiOperation("添加操作记录")
    @PostMapping("/addRecord")
    public Map addRecord (OrderoRecord orderoRecord){
        orderoRecordService.addRecord(orderoRecord);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }

    @ApiOperation("查询当前用户完成的具体订单")
    @PostMapping("/getToderList")
    public Map getToderList(@RequestParam("orderPhone") String phone,@RequestParam("orderNumber") String number){
        List<ServerAndTorderVo> torders = torderService.selectTorder(phone,number);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setData(torders);
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }

    @ApiOperation("查询当前用户被陪护员接单的订单")
    @PostMapping("/getToderListBeUsed")
    public Map getToderListBeUsed(@RequestParam("orderPhone") String phone){
        List<ServerAndTorderVo> torders = torderService.getTorderList(phone);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setData(torders);
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }
    @ApiOperation("查询当前用户陪护员服务中的订单")
    @PostMapping("/getToderListServiceIng")
    public Map getToderListServiceIng(@RequestParam("orderPhone") String phone){
        List<ServerAndTorderVo> torders = torderService.getTorderListServiceIng(phone);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setData(torders);
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }


    @ApiOperation("退款")
    @PostMapping("/refund")
    public Map refund(@RequestParam("orderNumber") String number,@RequestParam("refund") String refund) throws BusinessException {

        List<Torder> torders = torderService.selectByNumbers(number);
        if (torders.size()<0) {
            throw new BusinessException(ExceptionCode.TORDER_NULL);
        }
        Torder torder = torders.get(0);
        if ("免支付".equals(torder.getPayType())) {
            throw new BusinessException(ExceptionCode.REFUND_FALSE);
        }
        torderService.ToderRefund(number, refund);
        OrderoRecord orderoRecord = new OrderoRecord();
        orderoRecord.setOrderId(torder.getOrderId());
        orderoRecord.setOperateContent("退款");
        orderoRecord.setOperateRemark(torder.getCancelRemark());
        orderoRecord.setOperateUser(torder.getOrderUser());
        orderoRecordService.addRecord(orderoRecord);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }

    @ApiOperation("查询当前用户全部订单信息")
    @PostMapping("/getAllToderList")
    public Map getAllToderList(@RequestParam("phone") String phone) {
        List<ServerAndTorderVo> torders = torderService.selectAllTorderList(phone);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(torders);
        return jsonResult.getDataMap();
    }
    @ApiOperation("查询当前用户退款失败订单信息")
    @PostMapping("/getRefundFalse")
    public Map getRefundFalse(@RequestParam("phone") String phone) {
        List<ServerAndTorderVo> torders = torderService.selectRefundFalse(phone);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(torders);
        return jsonResult.getDataMap();
    }
    @ApiOperation("查询当前用户退款成功订单信息")
    @PostMapping("/getRefundTrue")
    public Map getRefundTrue(@RequestParam("phone") String phone) {
        List<ServerAndTorderVo> torders = torderService.selectRefundTrue(phone);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(torders);
        return jsonResult.getDataMap();
    }
    @ApiOperation("查询当前用户退款中订单信息")
    @PostMapping("/getRefunding")
    public Map getRefunding(@RequestParam("phone") String phone) {
        List<ServerAndTorderVo> torders = torderService.selectRefunding(phone);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(torders);
        return jsonResult.getDataMap();
    }
    @ApiOperation("查询当前用户退款成功具体订单信息")
    @PostMapping("/getRefundTrue1")
    public Map getRefundTrue1(@RequestParam("orderNumber") String orderNumber) {
        List<ServerAndTorderVo> serverAndTorderVos = torderService.selectRefundTrueByOrderNumber(orderNumber);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(serverAndTorderVos);
        return jsonResult.getDataMap();
    }
    @ApiOperation("查询当前用户退款失败具体订单信息")
    @PostMapping("/getRefundFalse2")
    public Map getRefundFalse2(@RequestParam("orderNumber") String orderNumber) {
        List<ServerAndTorderVo> serverAndTorderVos = torderService.selectRefundFalseByOrderNumber(orderNumber);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(serverAndTorderVos);
        return jsonResult.getDataMap();
    }
    @ApiOperation("查询当前用户等待系统分配的具体订单信息")
    @PostMapping("/getPaying")
    public Map getPaying(@RequestParam("orderNumber") String orderNumber) {
        List<ServerAndTorderVo> serverAndTorderVos = torderService.selectPayings(orderNumber);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(serverAndTorderVos);
        return jsonResult.getDataMap();
    }
    @ApiOperation("查询当前用户服务中的具体订单信息")
    @PostMapping("/getServerIng")
    public Map getServerIng(@RequestParam("orderNumber") String orderNumber) {
        List<ServerAndTorderVo> serverAndTorderVos = torderService.selectServering(orderNumber);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(serverAndTorderVos);
        return jsonResult.getDataMap();
    }

    @ApiOperation("确认订单")
    @PostMapping("/sureTorderFinshed")
    public Map sureTorderFinshed(@RequestParam("orderNumber") String orderNumber) throws BusinessException {
        List<Torder> torders = torderService.selectByNumbers(orderNumber);
        if (torders.size() <0) {
            throw new BusinessException(ExceptionCode.TORDER_NULL);
        }
        torders.get(0).setOrderState("已完成");
        torderService.sureTorderFinshed(torders.get(0));
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }






}
